﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace RunCamlSample.QueryBuilders
{
    /// <summary>
    /// Create By: Nguyen Thanh Tung
    /// </summary>
    public class ResultList
    {
        public static ResultList Parser(XmlNode node)
        {
            XmlDocument xmlResultsDoc = new XmlDocument();

            xmlResultsDoc.LoadXml(node.OuterXml);

            var ns = new XmlNamespaceManager(xmlResultsDoc.NameTable);

            ns.AddNamespace("z", "#RowsetSchema");

            return new ResultList(xmlResultsDoc.SelectNodes("//z:row", ns));
        }

        public List<Result> Lists { get; set; }

        public int Count
        {
            get { return Lists.Count; }
        }

        public ResultList(XmlNodeList nodeList)
        {
            Lists = new List<Result>(new ResultListWrapper(nodeList));
        }
    }

    internal class ResultListWrapper : IEnumerable<Result>, IEnumerator<Result>
    {
        private XmlNodeList nodeList;
        private readonly IEnumerator enumerator;
        
        public int Count { get; set; }

        public ResultListWrapper(XmlNodeList nodeList)
        {
            this.nodeList = nodeList;
            enumerator = nodeList.GetEnumerator();
            Count = nodeList.Count;
        }

        public IEnumerator<Result> GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Dispose()
        {
        }

        public bool MoveNext()
        {
            return enumerator.MoveNext();
        }

        public void Reset()
        {
            enumerator.Reset();
        }

        public Result Current
        {
            get {
                return new Result((XmlNode)enumerator.Current); 
            }
        }

        object IEnumerator.Current
        {
            get { return Current; }
        }
    }
    
    public class Result
    {
        private readonly XmlNode node;

        public Result(XmlNode current)
        {
            node = current;
        }

        public string ContentType
        {
            get
            {
                if (node.Attributes["ows_ContentType"] != null)
                    return node.Attributes["ows_ContentType"].Value;

                return string.Empty;
            }
        }

        public string BasePath
        {
            get
            {
                if (node.Attributes["ows_FileDirRef"] != null)
                    return node.Attributes["ows_FileDirRef"].Value.Split(new[]{";#"}, StringSplitOptions.RemoveEmptyEntries)[1];

                return string.Empty;
            }
        }

        public int Id
        {
            get
            {
                if (node.Attributes["ows_ID"] != null)
                    return Convert.ToInt32(node.Attributes["ows_ID"].Value);

                return 0;
            }
        }

        public string BaseName
        {
            get
            {
                if (node.Attributes["ows_BaseName"] != null)
                    return node.Attributes["ows_BaseName"].Value;

                return string.Empty;
            }
        }

        public string Title
        {
            get
            {
                if (node.Attributes["ows_Title"] != null)
                    return node.Attributes["ows_Title"].Value;

                return string.Empty;
            }
        }

        public string this[string index]
        {
            get
            {
                if (node.Attributes["ows_" + index] != null)
                    return node.Attributes["ows_" + index].Value;

                return string.Empty;
            }
        }
    }
}